﻿using System;
using System.Windows.Forms;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Controls;

namespace Outlook.WorkItems.Extension.Controls
{
    /// <summary>
    /// Display Work Item Form
    /// </summary>
    public partial class WorkItemForm : Form
    {
        #region Ctors

        private readonly bool _allowSave;

        /// <summary>
        /// Ctor
        /// </summary>
        public WorkItemForm()
        {
            InitializeComponent();
        }


        /// <summary>
        /// ctor
        /// </summary>
        /// <param name="store">Work Item Store</param>
        /// <param name="workItem">Work Item to display</param>
        /// <param name="allowSave"></param>
        public WorkItemForm(WorkItemStore store, WorkItem workItem, bool allowSave)
            : this()
        {
            m_store = store;
            m_workItem = workItem;
            _allowSave = allowSave;
        }

        /// <summary>
        /// ctor
        /// </summary>
        /// <param name="store">Work Item Store</param>
        /// <param name="workItem">Work Item to display</param>
        public WorkItemForm(WorkItemStore store, WorkItem workItem)
            : this()
        {
            m_store = store;
            m_workItem = workItem;
        }

        #endregion

        #region Fields

        private readonly WorkItem m_workItem;
        private WorkItemStore m_store;

        #endregion

        /// <summary>
        /// Form Load Event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void WIViewerForm_Load(object sender, EventArgs e)
        {
            var frm = new WorkItemFormControl
                          {
                              FormDefinition = m_workItem.DisplayForm,
                              Item = m_workItem,
                              ReadOnly = true
                          };

            panel1.Controls.Add(frm);
            frm.Dock = DockStyle.Fill;

            if (_allowSave)
            {
                button1.Text = "Save";
            }
            else
            {
                button1.Text = "OK";
            }
        }
    }
}
